package yz;

import java.util.LinkedList;
import java.util.Queue;

/**
 * [225. 用队列实现栈](https://leetcode-cn.com/problems/implement-stack-using-queues/)
 *
 * @Author yangz
 * @Date 2022/11/13 20:02
 */
public class LeetCode225_ImplementStackUsingQueues {
    Queue<Integer> queue1;
    Queue<Integer> queue2;

    public LeetCode225_ImplementStackUsingQueues() {
        queue1 = new LinkedList<Integer>();
        queue2 = new LinkedList<Integer>();
    }

    public void push(int x) {
        queue2.offer(x);
        while (!queue1.isEmpty()) {
            queue2.offer(queue1.poll());
        }
        Queue<Integer> temp = queue1;
        queue1 = queue2;
        queue2 = temp;
    }

    public int pop() {
        return queue1.poll();
    }

    public int top() {
        return queue1.peek();
    }

    public boolean empty() {
        return queue1.isEmpty();
    }
}
